import React, { useState,useEffect } from 'react'
import './App.css'
import HelloComponents from './hello'
import NameEditComponent from './nameEdit' 
import {Color} from './model/color'
import ColorBrowser from './ColorBrowser'

// 根组件 没有特别的需求的话 不加React.FC
function App() {
  // ts 为大型项目而来 容不了一点bug
  // 每一步都很严格 类型约束或者接口
  const [color,setColor]=useState<Color>({
    red:205,
    green:210,
    blue:100,
  })
  const [name,setName]=useState('defaultUserName')
  const [editingName,setEditingName]=useState('defaultUserName')
  // const setUsernameState=
  //   (e:React.ChangeEvent<HTMLInputElement>)=>{
  //     setName(e.target.value)
  //   }
  const setUsernameState=()=>{
    setName(editingName);
  }
  const loadUserName=()=>{
    setTimeout(()=>{
      setName('name from async call')
      setEditingName("name from async call")
    },2500)
  }
  useEffect(()=>{
    loadUserName()
  },[])
  return (
    <div className="App">

      <ColorBrowser color={color}/>
      <HelloComponents userName={name} userAge={18}/>
      <NameEditComponent 
        initialUserName={name}
        editingName={editingName}
        onNameUpdated={setUsernameState}
        onEditingNameUpdated={setEditingName}
        disabled={editingName === ''||editingName===name}
      />
    </div>
  )
}

export default App
